// pages/operat/fleedTools/outStorage/outStorage.js
const LApp = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
   outStorageModel: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = LApp.pages.get("fleedTools/outStorage/scanCodes/scanCodes").data;
this.setData({
  outStorageModel: data.outInfo
}) 
if (this.data.outStorageModel.ProductID!=0)
{
  this.productShow(this.data.outStorageModel.ProductID);
}
else
  this.getproductList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   this.GetFormCode();
   this.getsalesAreaList();
   this.getWarehouseList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  GetFormCode: function (data) {
    var formCode = LApp._formatTime("yyyyMMddhhmmss");
    if (data) {
      formCode = data + "-" + formCode;
    }
    this.data.outStorageModel.FormCode = formCode;
    this.setData(this.data);
  },
  //片区下拉框
  getsalesAreaList: function () {
   let _this = this;
   LApp._get({
    url: "nbapi/salesarea/selectitem",
    data: { userId: LApp.globalData.userInfo.UserID },
    success: function (r) {
     _this.setData({
      salesAreaList: r
     })
    }
   })
  },
  salesAreaSelect: function (e) {
   var _salesArea = this.data.salesAreaList[e.detail.value];
   if (_salesArea) {
    this.data.outStorageModel.areaName = _salesArea.Name;
    this.data.outStorageModel.SaleAreaId = _salesArea.Code;
    this.data.outStorageModel.Name = undefined;
    this.data.outStorageModel.Distributor = undefined;
    this.setData(this.data);
    this.getdistributorList(this.data.outStorageModel.SaleAreaId);
    var proId = this.data.outStorageModel.ProductID;
    proId = proId == undefined ? 0 : proId;
    var dId = this.data.outStorageModel.Distributor;
    dId = dId == undefined ? 0 : dId;
    this.GetFormCode(_salesArea.Code + "-" + dId + "-" + proId);
   }
  },

  //经销商下拉框
  getdistributorList: function (__areaId) {
   let _this = this;
   LApp._get({
    url: 'nbapi/distributor/dselectitem',
    data: { areaId: __areaId, userId: LApp.globalData.userInfo.UserID },
    success: function (r) {
     _this.setData({
      distributorList: r
     })
    }
   })
  },
  distributorSelect: function (e) {
   var _distributor = this.data.distributorList[e.detail.value];
   if (_distributor) {
    this.data.outStorageModel.Name = _distributor.Name;
    this.data.outStorageModel.Distributor = _distributor.Code;
    this.setData(this.data);
    var proId = this.data.outStorageModel.ProductID;
    proId = proId == undefined ? 0 : proId;
    var aId = this.data.outStorageModel.SaleAreaId;
    aId = aId == undefined ? 0 : aId;
    this.GetFormCode(aId + "-" + _distributor.Code + "-" + proId);
   }
  },
  //仓库下拉框
  getWarehouseList: function () {
   let _this = this;
   LApp._get({
    url: 'nbapi/warehouse/wselectitem',
    data: { userId: LApp.globalData.userInfo.UserID},
    success: function (r) {
     _this.setData({
      warehouseList: r
     })
    }
   })
  },
  warehouseSelect: function (e) {
   var _warehouse = this.data.warehouseList[e.detail.value];
   if (_warehouse) {
    this.data.outStorageModel.wName = _warehouse.Name;
    this.data.outStorageModel.WarehouseId = _warehouse.Code;
    this.setData(this.data);
   }
  },
  //产品下拉框
  getproductList: function () {
   let _this = this;
   LApp._get({
    url: 'nbapi/product/pselectitem',
    data: { userId: LApp.globalData.userInfo.UserID },
    success: function (r) {
     _this.setData({
      productList: r
     })
    }
   })
  },
  productSelect: function (e) {
   var _product = this.data.productList[e.detail.value];
   if (_product) {
    this.data.outStorageModel.pName = _product.Name;
    this.data.outStorageModel.ProductID = _product.Code;
    this.setData(this.data);
    var dId = this.data.outStorageModel.Distributor;
    dId = dId == undefined ? 0 : dId;
    var aId = this.data.outStorageModel.SaleAreaId;
    aId = aId == undefined ? 0 : aId;
    this.GetFormCode(aId + "-" + dId + "-" + _product.Code);
   }
  },
  productShow:function(proId){
    let _this = this;
    LApp._get({
      url: 'nbapi/product/showproductinfo',
      data: { proId: proId} ,
      success: function (res) {
        _this.data.outStorageModel.pName = res.ProductName;
        _this.data.outStorageModel.ProductID = res.Id;
        _this.setData(_this.data);
      }
    })
  },
 
  bindModelChange: function (e) {
   wx.$$apply(e, this.data.outStorageModel);
  },
  //save
  saveStorag: function () {
   if (this.data.outStorageModel.SaleAreaId == undefined || this.data.outStorageModel.SaleAreaId == 0) {
    wx.showModal({
     title: '提示',
     content: '请选择片区',
     showCancel: false
    })
    return;
   }
   else if (this.data.outStorageModel.Distributor == undefined || this.data.outStorageModel.Distributor == 0) {
    wx.showModal({
     title: '提示',
     content: '请选择经销商',
     showCancel: false
    })
    return;
   }
   else if (this.data.outStorageModel.ProductID == undefined || this.data.outStorageModel.ProductID == 0) {
    wx.showModal({
     title: '提示',
     content: '请选择产品名称',
     showCancel: false
    })
    return;
   }
   else if (this.data.outStorageModel.WarehouseId == undefined || this.data.outStorageModel.WarehouseId == 0) {
    wx.showModal({
     title: '提示',
     content: '请选择出货仓库！',
     showCancel: false
    })
    return;

   }else{
    this.data.outStorageModel.FormType=2;
    this.data.outStorageModel.CreateLoginID = LApp.globalData.userInfo.UserID;
    this.data.outStorageModel.CreateLoginName = LApp.globalData.userInfo.LoginName;
    this.data.outStorageModel.EnterpriseID = LApp.globalData.userInfo.EnterpriseId;
   LApp._post({
    url: 'nbapi/Storage/pinStorage/add',
    data: this.data.outStorageModel,
    success: function (r) {
      wx.showModal({
        title: '保存成功！',
        icon: "success",
        success: function (res) {
          if (!res.cancel) {
            // wx.redirectTo({
            //   url: 'scanCodes/scanCodes',
            // })
            var pages = getCurrentPages();
            var currPage = pages[pages.length - 1];   //当前页面
            var prevPage = pages[pages.length - 2];  //上一个页面
            prevPage.setData({
              myDate: 1
            });
            wx.navigateBack(1);
          }
        }
      })
    }
   })
   }
  },
  goSearchCode: function () {
   if (this.data.outStorageModel) {
    LApp.pages.add(this);
    wx.navigateTo({
     url: 'searchCode/searchCode',
    })
   }
  }
}) 
